﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace DataCenter.Utility
{
    public partial class TypeEditForm : Form
    {
        private TypeEditForm()
        {
            InitializeComponent();
        }
        public TypeEditForm(SqlCommand command)
            : this()
        {
            this._Command = command;
        }
        public TypeEditForm(SqlCommand command, int typeId, string typeName)
            :this(command)
        {
            this.typeId = typeId;
            this.typeName = typeName;
            this.IsModlify = true;
        }
        private SqlCommand _Command;
        private bool IsModlify;
        private int typeId;
        private string typeName;

        public int TypeId
        {
            get { return typeId; }
            set { typeId = value; }
        }
        public string TypeName
        {
            get { return typeName; }
            set { typeName = value; }
        }

        private void TypeEditForm_Load(object sender, EventArgs e)
        {
            if (IsModlify)
            {
                txtTypeId.Enabled = false;
                txtTypeId.Text = typeId.ToString();
                txtTypeName.Text = typeName;
            }
            else
            {
                int typeId = SearchMaxId() + 1;
                txtTypeId.Text = Convert.ToString(typeId);
                txtTypeName.Text = "对象类型" + typeId;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                typeId = Convert.ToInt32(txtTypeId.Text);
                typeName = txtTypeName.Text.Trim();

                if (IsModlify) Modify();
                else Insert();

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private bool Check()
        {
            bool checkResult = true;
            string errmsg = "";
            Control ctrl = null;

            if (string.IsNullOrEmpty(txtTypeId.Text))
            {
                errmsg = "类型编号不能为空";
                ctrl = txtTypeId;
                checkResult = false;
                goto ShowError;
            }

            int iValue;
            if (!int.TryParse(txtTypeId.Text, out iValue))
            {
                errmsg = "类型编号必须为有效的整数";
                ctrl = txtTypeId;
                checkResult = false;
                goto ShowError;
            }

            if (string.IsNullOrEmpty(txtTypeName.Text))
            {
                errmsg = "类型名称不能为空";
                ctrl = txtTypeId;
                checkResult = false;
                goto ShowError;
            }

        ShowError:
            if (checkResult == false)
            {
                MessageBox.Show(errmsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (ctrl != null) ctrl.Focus();
            }

            return checkResult;
        }

        private bool IsRepeated(int typeId)
        {
            string commandString = "SELECT TypeId FROM ObjectType WHERE TypeId = " + typeId;
            _Command.CommandText = commandString;
            _Command.CommandType = CommandType.Text;
            object oValue = _Command.ExecuteScalar();
            return (oValue != null && oValue != DBNull.Value);
        }

        private void Modify()
        {
            string commandString = "UPDATE ObjectType SET TypeName=@TypeName WHERE TypeId=@TypeId";
            _Command.CommandText = commandString;
            _Command.CommandType = CommandType.Text;
            _Command.Parameters.AddWithValue("@TypeId", typeId);
            _Command.Parameters.AddWithValue("@TypeName", typeName);
            _Command.ExecuteNonQuery();
            _Command.Parameters.Clear();
        }

        private void Insert()
        {
            string commandString = "INSERT INTO ObjectType(TypeId, TypeName) VALUES(@TypeId, @TypeName)";
            _Command.CommandText = commandString;
            _Command.CommandType = CommandType.Text;
            _Command.Parameters.AddWithValue("@TypeId", typeId);
            _Command.Parameters.AddWithValue("@TypeName", typeName);
            _Command.ExecuteNonQuery();
            _Command.Parameters.Clear();
        }

        public static void Drop(SqlCommand command, int typeId)
        {
            string commandString = "DELETE FROM ObjectType WHERE TypeId=@TypeId";
            command.CommandText = commandString;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@TypeId", typeId);
            command.ExecuteNonQuery();
            command.Parameters.Clear();
        }

        private int SearchMaxId()
        {
            string commandString = "SELECT MAX(TypeId) FROM [ObjectType]";
            _Command.CommandText = commandString;
            _Command.CommandType = CommandType.Text;
            object oValue = _Command.ExecuteScalar();
            if (oValue != null && oValue != DBNull.Value)
                return Convert.ToInt32(oValue);
            return 0;
        }
    }
}
